import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 从zip获取文件
 * @author Sea
 */
public class Utils {
    public static void getFileFromZip(String zipPath, String saveFilePath) {
        try {
            ZipFile zf = new ZipFile(zipPath);
            FileWriter file = new FileWriter(saveFilePath);
            Enumeration enu = zf.entries();
            while(enu.hasMoreElements()){
                ZipEntry entry = (ZipEntry)enu.nextElement();
                String fileName = entry.getName();
                int size = (int)entry.getSize();
                System.out.println("size:" + size + "===" + "fileName:" + fileName);
                // 判断文件名和后缀
                if (fileName != null && fileName.endsWith(".log") && fileName.equals("001.log")) {
                    // 获取字节流
                    InputStream inputStream = zf.getInputStream(entry);
                    // 如果文件过大，需要分多次读取
                    byte[] buffer = new byte[size];
                    int read = inputStream.read(buffer);
                    file.write(new String(buffer,0, read));
                    file.flush();
                    break;
                }
            }
            zf.close();
            file.close();
        } catch (IOException e) {
            System.out.println("打开zip文件失败");
        }
    }
}


